﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Products_ctrProductCategory : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblWarning.Text = MultipleLanguage.SEARCH_NOITEM[nCurrentLang - 1];  
            this.InitData();
            this.BindData(1);
        }
    }
    private void InitData()
    {
        try
        {
            int nCatID = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_CAT ]);
            string sKeyword = ConvertData.ConvertToString(Request.QueryString[Config.KEYWORD]);
            int nTradeMark = ConvertData.ConvertToInt(Request.QueryString[Config.TRADE_MARK]);
            int nType = ConvertData.ConvertToInt(Request.QueryString[Config.TYPE]);
            int nBestSeller = ConvertData.ConvertToInt(Request.QueryString[Config.BESTSELLER]);
            if (nBestSeller > 0)
            {
                lblCategoryName.Text = MultipleLanguage.LBL_BESTSELLER[nCurrentLang - 1].ToUpper();  
            }
            if (nCatID >0)
            {
                ProductCategory objProductCat = new ProductCategory();
                objProductCat.LoadById(nCatID);
                switch (nCurrentLang)
                { 
                    case 1:
                        lblCategoryName.Text = objProductCat.Data.ProductCategoryName1.ToUpper() ;
                        break;
                    case 2:
                        lblCategoryName.Text = objProductCat.Data.ProductCategoryName2.ToUpper();
                        break;
                    case 3:
                        lblCategoryName.Text = objProductCat.Data.ProductCategoryName3.ToUpper();
                        break;
                }
                
            }
            if(sKeyword.Length >0)
            {
                lblCategoryName.Text = MultipleLanguage.LBL_KEYWORD[nCurrentLang -1].ToUpper() + ":" + " " +  sKeyword.Replace("-", " ").ToUpper ();
            }
            if (nTradeMark > 0)
            {
                ProductTradeMark objProductTradeMark = new ProductTradeMark();
                objProductTradeMark.LoadById(nTradeMark);
                lblCategoryName.Text = objProductTradeMark.Data.TradeMarkName.ToUpper();
               
            }
            if (nType > 0)
            {
                ProductCategoryType objType = new ProductCategoryType();
                objType.LoadById(nType);
                switch (nCurrentLang)
                { 
                    case 1:
                        lblCategoryName.Text = objType.Data.Description1.ToUpper();
                        break;
                    case 2:
                        lblCategoryName.Text = objType.Data.Description2.ToUpper();
                        break;
                    case 3:
                        lblCategoryName.Text = objType.Data.Description3.ToUpper();
                        break;
                }
            }

            string sLabel = lblCategoryName.Text;

            if (sLabel.Length == 0)
            { lblCategoryName.Text = MultipleLanguage.PRODUCT[nCurrentLang - 1].ToUpper(); }
            
        }
        catch {
            Response.Redirect("~/Default.aspx");
        } 
         
    }
    private void BindData(int CurrentPageIndex)
     {
        int nCategory = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_CAT]);

        ProductCategory objProductCategory = new ProductCategory();
        DataTable dtbProductCategory = new DataTable();
        dtbProductCategory = objProductCategory.SearchByParent(nCategory);
        int nCount = dtbProductCategory.Rows.Count;
        string sParent = "";
        sParent = ConvertData.ConvertToString(nCategory);
        if (nCount > 0)
        {
          
            for (int i = 0; i < nCount; i++)
            {
                sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory.Rows[i]["ProductCategoryID"]) + ",";

                ProductCategory objProductCategory2 = new ProductCategory();
                DataTable dtbProductCategory2 = new DataTable();
                   dtbProductCategory2 = objProductCategory2.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory.Rows[i]["ProductCategoryID"]) );
                  int nCount2 = dtbProductCategory2.Rows.Count;
                  if (nCount2 > 0)
                  {

                      for (int y = 0; y < nCount2; y++)
                      {
                          sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory2.Rows[y]["ProductCategoryID"]) + ",";

                          ProductCategory objProductCategory3 = new ProductCategory();
                          DataTable dtbProductCategory3 = new DataTable();
                          dtbProductCategory3 = objProductCategory3.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory2.Rows[y]["ProductCategoryID"]));
                          int nCount3 = dtbProductCategory3.Rows.Count;
                          if (nCount3 > 0)
                          {
                              for (int z = 0; z < nCount3; z++)
                              {
                                  sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory3.Rows[z]["ProductCategoryID"]) + ",";

                                  ProductCategory objProductCategory4 = new ProductCategory();
                                  DataTable dtbProductCategory4 = new DataTable();
                                  dtbProductCategory4 = objProductCategory4.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory3.Rows[z]["ProductCategoryID"]));
                                  int nCount4 = dtbProductCategory4.Rows.Count;
                                  if (nCount4 > 0)
                                  {
                                      for (int z1 = 0; z1 < nCount4; z1++)
                                      {
                                          sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory4.Rows[z1]["ProductCategoryID"]) + ",";
                                      }
                                  }
                              }
                          }
                      }
                  }
            }
            sParent = sParent.Replace(",,", ",");
            sParent = ConvertData.TruncateString(sParent, sParent.Length - 1);
        }

        string sKeyword = ConvertData.ConvertToString(Request.QueryString[Config.KEYWORD ]);
        int nTradeMark = ConvertData.ConvertToInt(Request.QueryString[Config.TRADE_MARK]);
        int nType = ConvertData.ConvertToInt(Request.QueryString[Config.TYPE]);

        int nBestSeller = 0;
        if (string.IsNullOrEmpty(Request.QueryString[Config.BESTSELLER]))
        {
            nBestSeller = -1;
        }
        else
        {
            nBestSeller = ConvertData.ConvertToInt(Request.QueryString[Config.BESTSELLER]);
        }
        int nMaterial = 0;
        double dPriceFrom = 0;
        double dPriceTo = 100000000;
        int nStatus = 1;
        int nPageCount = 0;
        int nItemView = 0;
        int nItemID = 0;
        int isSaleOff = -1;
        //string sProductName = "";
        //int nPageIndex = 1;
        int nPageSize =20;
        Products objProduct = new Products();
        DataTable dtbProduct = new DataTable();
        dtbProduct = objProduct.Search(nItemView, "", sKeyword, nItemID, sParent, nTradeMark, nMaterial, dPriceFrom, dPriceTo, isSaleOff, nBestSeller, nStatus, 0, 0, 0, nDefaultLanguage, nCurrentLang, CurrentPageIndex, nPageSize, ref nPageCount);
        rptProduct.DataSource = dtbProduct;
        rptProduct.DataBind();

        if (nPageCount > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
      
        Pager.Visible = true;
        VisiblePaging(nPageCount);
        Pager.ItemCount = nPageCount;
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string sCode = string.Empty;
        string sContact = string.Empty;
        string sPrice = string.Empty;
        int nCurrentLang = Support.GetCurrentLanguage();

        sPrice = MultipleLanguage.PRICE[nCurrentLang - 1] + ": ";
        sContact = MultipleLanguage.PRICE_CALL[nCurrentLang - 1];
        sCode = MultipleLanguage.PROCODE[nCurrentLang - 1];


        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnProductImage = (ImageButton)e.Item.FindControl("btnProductImage");
            HyperLink lnkProductName = (HyperLink)e.Item.FindControl("lnkProductName");
            Label lblCode = (Label)e.Item.FindControl("lblCode");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");
            LinkButton lnkAddCart = (LinkButton)e.Item.FindControl("lnkAddCart");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            string sProductName = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName"));
            if (sProductName.Length > 0)
            {
                lnkProductName.Text = sProductName;
            }
            else
            {
                lnkProductName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName_Default"));
            }
            lblCode.Text = sCode + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));
            string sToolTip = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MainContent"));
            if (sToolTip.Length > 0)
            {
                btnProductImage.ToolTip = sToolTip;
            }
            else
            { btnProductImage.ToolTip = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MainContent_Default")); }
            double dPrice = ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
            if (dPrice > 0)
            { lblPrice.Text = sPrice + ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "Price"))) + " " + "₫"; }
            else { lblPrice.Text = sPrice + sContact; }

            lnkProductName.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?" + Config.PRO_ID + "=" + nID;
            lnkAddCart.Text = MultipleLanguage.BUY[nCurrentLang - 1];
            //lnkAddCart.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ADD_BASKET_SUCCCESS[nCurrentLang - 1]);
            btnProductImage.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD   + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageDefault"));
            btnProductImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lnkAddCart.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
        }
    }
    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?" + Config.PRO_ID + "=" + nID;
                Response.Redirect(sDetail);
                break;
            case "addcart":
                // Add product to Cart
                clsBasket objBasket = clsBasket.getclsBaskets(Context);
                ItemclsBaskets item = new ItemclsBaskets(nID);
                objBasket.AddItem(item);
                int nCat = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_CAT]);
                if (nCat > 0)
                { Response.Redirect(this.GetCurrentPageName() + "?" + Config.PRO_CAT + "=" + nCat); }
                else
                {
                    Response.Redirect(this.GetCurrentPageName());
                }
                break;
        }
    }

    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    } 

    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.BindData(currnetPageIndx);
    }
    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = 20;
        if (intPagecount > nPageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }
}
